package com.heima.article.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private ApArticleContentMapper contentMapper;

    @Autowired
    private ApArticleConfigMapper configMapper;

    // 单页最大加载的数字
    private final static short MAX_PAGE_SIZE = 50;
    /**
     * 查询更多的老数据
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult load(ArticleHomeDto dto, Short type) {
        //1.校验参数
        Integer size = dto.getSize();
        if(size == null || size == 0){
            size = 10;
        }
        size = Math.min(size,MAX_PAGE_SIZE);
        dto.setSize(size);

        //文章频道校验
        if(StringUtils.isEmpty(dto.getTag())){
            dto.setTag(ArticleConstants.DEFAULT_TAG);
        }

        //时间校验
        if(dto.getMaxBehotTime() == null) dto.setMaxBehotTime(new Date());
        if(dto.getMinBehotTime() == null) dto.setMinBehotTime(new Date());

        //调用mapper查询数据
        List<ApArticle> list = apArticleMapper.list(dto,type);

        //封装结果并返回
        return ResponseResult.okResult(list);
    }

    /**
     * 处理自媒体段发布过来的文章数据
     * 新增或者修改文章数据
     *
     * @param articleDto
     * @return
     */
    @Override
    @Transactional
    public ResponseResult saveOrUpdateArticle(ArticleDto articleDto) {
        //1. 参数校验
        if (null == articleDto || org.apache.commons.lang.StringUtils.isEmpty(articleDto.getContent())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2. 判断是否有id属性 如果没有则走新增  如果有则走修改
        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(articleDto, apArticle);

        if (null == articleDto.getId()) {
            //新增
            apArticleMapper.insert(apArticle);

            ApArticleConfig config = new ApArticleConfig();
            config.setArticleId(apArticle.getId());
            config.setIsComment(Boolean.TRUE);
            config.setIsDown(Boolean.FALSE);
            config.setIsDelete(Boolean.FALSE);
            config.setIsForward(Boolean.TRUE);
            configMapper.insert(config);

            ApArticleContent content = new ApArticleContent();
            content.setArticleId(apArticle.getId());
            content.setContent(articleDto.getContent());
            contentMapper.insert(content);
        } else {
            //修改
            apArticleMapper.updateById(apArticle); //修改文章基本数据

            ApArticleContent content = contentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery()
                    .eq(ApArticleContent::getArticleId, apArticle.getId()));
            content.setContent(articleDto.getContent());

            contentMapper.updateById(content);
        }

        //3. 返回文章的id
        return ResponseResult.okResult(apArticle.getId());
    }

}
